﻿@model jQuery.InfiniteScroll.Models.ProductsViewModel
@helper Pager(int currentPageIndex, int pageRecordsCount, int totalRecordsCount) {
    
    int totalPagesCount = (int)Math.Ceiling((double)totalRecordsCount / (double)pageRecordsCount);
    int numberOfPagesToDisplay = 10;

    int firstDisplayedPageIndex = 1;
    int lastDisplayedPageIndex = totalPagesCount;
    if (totalPagesCount > numberOfPagesToDisplay)
    {
        int middleDisplayedPageIndex = (int)Math.Ceiling((double)numberOfPagesToDisplay / 2d) - 1;
        firstDisplayedPageIndex = (currentPageIndex - middleDisplayedPageIndex);
        lastDisplayedPageIndex = (currentPageIndex + middleDisplayedPageIndex);
        if (firstDisplayedPageIndex < 4)
        {
            lastDisplayedPageIndex = numberOfPagesToDisplay;
            firstDisplayedPageIndex = 1;
        }
        else if (lastDisplayedPageIndex > (totalPagesCount - 4))
        {
            lastDisplayedPageIndex = totalPagesCount;
            firstDisplayedPageIndex = (totalPagesCount - numberOfPagesToDisplay + 1);
        }
    }
    
    <div id="pager" class="ui-helper-clearfix">
    @if (currentPageIndex > 1)
    {
        @Html.ActionLink("<", "Products", new { page = currentPageIndex - 1 }, new { @class = "ui-state-default ui-corner-all" })
    }
    else
    {
        <span class="ui-state-default ui-corner-all ui-state-disabled">&lt;</span>
    }
    @if (firstDisplayedPageIndex > 1)
    {
        @Html.ActionLink("1", "Products", new { page = 1 }, new { @class = "ui-state-default ui-corner-all" })
        if (firstDisplayedPageIndex > 3)
        {
            @Html.ActionLink("2", "Products", new { page = 2 }, new { @class = "ui-state-default ui-corner-all" })
        }
        if (firstDisplayedPageIndex > 2)
        {
            <span>...</span>
        }
    }
    @for (int displayedPageIndex = firstDisplayedPageIndex; displayedPageIndex <= lastDisplayedPageIndex; displayedPageIndex++)
    {
        if (displayedPageIndex == currentPageIndex || (currentPageIndex <= 0 && displayedPageIndex == 0))
        {
            @Html.Raw(String.Format("<span class=\"ui-state-hover ui-corner-all\">{0}</span>", displayedPageIndex))
        }
        else
        {
            @Html.ActionLink(displayedPageIndex.ToString(), "Products", new { page = displayedPageIndex }, new { @class = "ui-state-default ui-corner-all" })
        }
    }
    @if (lastDisplayedPageIndex < totalPagesCount)
    {
        if (lastDisplayedPageIndex < totalPagesCount - 1)
        {
            <span>...</span>
        }
        if (totalPagesCount - 2 > lastDisplayedPageIndex)
        {
            @Html.ActionLink((totalPagesCount - 1).ToString(), "Products", new { page = totalPagesCount - 1 }, new { @class = "ui-state-default ui-corner-all" })
        }
        @Html.ActionLink(totalPagesCount.ToString(), "Products", new { page = totalPagesCount }, new { @class = "ui-state-default ui-corner-all" })
    }
    @if (currentPageIndex < totalPagesCount)
    {
        @Html.ActionLink(">", "Products", new { page = currentPageIndex + 1 }, new { @class = "ui-state-default ui-corner-all" })
    }
    else
    {
        <span class="ui-state-default ui-corner-all ui-state-disabled">&gt;</span>
    }
    </div>
}
<div id="list">
    @foreach (Northwind.Model.Product product in Model.Products)
    {
        <div class="ui-corner-all">
            <strong>Name:</strong> @product.Name<br />
            <strong>Category:</strong> @product.Category.Name<br />
            <strong>Supplier:</strong> @product.Supplier.Name<br />
            <strong>Unit price:</strong> @product.UnitPrice<br />
            <strong>Quantity per unit:</strong> @product.QuantityPerUnit<br />
            <strong>Units in stock:</strong> @product.UnitsInStock<br />
        </div>
    }
</div>
@Pager(Model.CurrentPageIndex, Model.PageRecordsCount, Model.TotalRecordsCount)